/***************************************** 
 * Desc: UGUI通用文本，一般不用挂，只有需求频繁又藏得深的文本才交给框架托管否则直接Find就可以了
 * @Author: GSan
 * @Email: 2087653648@qq.com
 * CreateDate: 2020-07-21 14:54:13
*****************************************/

using Common.UGUI.Base;
using UnityEngine;
using UnityEngine.UI;

namespace Common.UGUI.View {

    [RequireComponent(typeof(Text))]
    public class UGUIText : UGUIView {

        #region 域

        /// <summary>
        /// 文本组件
        /// </summary>
        public Text Text {

            get {

                if (_text == null) {

                    _text = GameObject.GetComponent<Text>();
                }

                return _text;
            }
        }

        private Text _text = null;
        private Color _currentColor = default(Color);
        private Vector3 _oldScale = Vector3.one;

        #endregion

        #region 覆盖

        protected override void OnAwake() {

            _text = GameObject.GetComponent<Text>();

            if(_text != null) {

               SetColor(_text.color);
            }

            SetScale(RectTransform.localScale);
        }

        protected override void OnDestroyByMono() {

            _text = null;
        }

        public override void SetActive(bool isActive) {

            _isActive = isActive;

            if (_isActive) {

                if (OnEnable != null) {

                    OnEnable();
                }

#if UNITY_EDITOR
                if (!Application.isPlaying) {

                    RectTransform.localScale = Vector3.one;

                    return;
                }
#endif

                Text.color = _currentColor;

                RectTransform.localScale = _oldScale;
            } else {

                if (OnDisable != null) {

                    OnDisable();
                }

#if UNITY_EDITOR
                if (!Application.isPlaying) {

                    RectTransform.localScale = Vector3.zero;

                    return;
                }
#endif

                Text.color = Color.clear;

                RectTransform.localScale = Vector3.zero;
            }
        }

        #endregion

        #region 接口

        /// <summary>
        /// 设置内容
        /// </summary>
        public void SetText(string content) {

            Text.text = content;
        }

        /// <summary>
        /// 设置字体大小
        /// </summary>
        public void SetFonsize(int fontSize) {

            Text.fontSize = fontSize;
        }

        /// <summary>
        /// 设置字体对齐方式
        /// </summary>
        public void SetAlign(TextAnchor textAnchor) {

            Text.alignment = textAnchor;
        }

        /// <summary>
        /// 设置字体颜色，一定要用该函数否则可能出现奇怪表现
        /// </summary>
        public void SetColor(Color color) {

            _currentColor = color;

            if (_isActive) {

                Text.color = _currentColor;
            }
        }

        /// <summary>
        /// 设置字体缩放，一定要用该函数否则可能出现奇怪表现
        /// </summary>
        public void SetScale(Vector3 scale) {

            _oldScale = scale;

            if (_isActive) {

                RectTransform.localScale = _oldScale;
            }
        }

#endregion
    }
}
